package com.aivin.lib_reader.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aivin.lib_widget.WkExpandLayout;
import com.aivin.lib_reader.R;
import com.aivin.lib_reader.adapter.RecyclerViewAdapter;
import com.aivin.lib_widget.bean.MarginDecoration;

import java.util.ArrayList;

import com.aivin.lib_reader.icallback.ColorSelectCallBack;
import com.aivin.lib_reader.icallback.SettingCallBack;



public class ReadSettingPopuWindow extends PopupWindow implements View.OnClickListener {
    private Context mCtx;
    private SettingCallBack settingCallBack ;
    private WkExpandLayout wkExpandPhotoForm;
    private int dataSize ;

    public ReadSettingPopuWindow(Context context  , int dataSize  ,SettingCallBack settingCallBack  ) {
        super(context);
        this.mCtx = context;
        this.dataSize = dataSize;
        this.settingCallBack= settingCallBack ;
        View rootView = initView(context);
        this.setContentView(rootView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 弹出窗体的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 弹出窗体的高
        this.setOutsideTouchable(false);// 弹出窗体可点击
        ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);// //实例化一个ColorDrawable颜色为XX
        this.setBackgroundDrawable(dw);
    }

    private View initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popuwindowRoot = inflater.inflate(R.layout.settingpopulay, null);

        TextView contentLenth = popuwindowRoot.findViewById(R.id.contentLenth) ;
        String contentSize =  String.format ( mCtx.getString(R.string.str_contentSize), dataSize) ;
        contentLenth.setText(contentSize);
        wkExpandPhotoForm = popuwindowRoot.findViewById(R.id.wkExpandPhotoForm);
        wkExpandPhotoForm.initExpandViewConfig(true ,0 ,200);
        ArrayList<Integer> colorDatas = new ArrayList<>();
        colorDatas.add( Color.parseColor("#FFFFFF") );
        colorDatas.add( Color.parseColor("#FAF9DE") );
        colorDatas.add( Color.parseColor("#E3EDCD") );
        colorDatas.add( Color.parseColor("#DCE2F1") );
        colorDatas.add( Color.parseColor("#E9EBFE") );
        colorDatas.add( Color.parseColor("#EAEAEF") );

        RecyclerView recyclerView = popuwindowRoot.findViewById( R.id.colorRecycle);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 6));
        recyclerView.addItemDecoration(new MarginDecoration(context));
        recyclerView.setAdapter(new RecyclerViewAdapter( colorDatas, new ColorSelectCallBack() {

            @Override
            public void onSelect(int color, int itemIndex) {
                if(settingCallBack!=null){
                    settingCallBack.onColorChange(color);
                }
            }
        }));



        SeekBar seekbarReaderSize = popuwindowRoot.findViewById(R.id.seekbarReaderSize) ;
        setMinAndMax(seekbarReaderSize ,80 ,150 , 100) ;
        popuwindowRoot.findViewById(R.id.closeWindow).setOnClickListener(this);
        popuwindowRoot.findViewById(R.id.colorSettingShow).setOnClickListener(this);

        popuwindowRoot.findViewById(R.id.prepaper).setOnClickListener(this);
        popuwindowRoot.findViewById(R.id.nextpaper).setOnClickListener(this);
        return popuwindowRoot ;
    }

    /**
     *  [30 ,500]
     */
    private void setMinAndMax(  SeekBar seekBar ,int min , int max  ,int defaulValue){
        seekBar.setMax(max -min);
        seekBar.setProgress(defaulValue -min);
        seekBar.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int data =   progress + min ;
                settingCallBack.onValueChange(data ,100);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        }) ;

    }

    @Override
    public void onClick(View v)  {
        int i = v.getId();
        if (i == R.id.closeWindow) {
            this.dismiss();
        } else if (i == R.id.prepaper) {
            settingCallBack.readPreData();
        } else if (i == R.id.nextpaper) {
            settingCallBack.readNextData();
        } else if (i == R.id.colorSettingShow) {
            if (wkExpandPhotoForm != null) {
                wkExpandPhotoForm.toggleExpand(false);
            }
        }
    }//

}